//
//  YAYIPersoninfoViewController.m
//  yayi
//
//  Created by Veer on 16/3/18.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIPersoninfoViewController.h"
#import "YAYIEditNameViewController.h"
#import "YAYISkillSelectViewController.h"
#import "YAYIEditBriefViewController.h"
#import "YAYICertificationViewController.h"

#import "YAYIPersoninfoSetCell.h"
#import "YAYIPersoninfoHeadCell.h"
#import "YAYIShowSkillView.h"
#import "YAYISettingSectionModel.h"
#import "YAYISettingItemModel.h"
#import "YAYIActionSheet.h"


#import "YAYIUserinfoModel.h"
#import "FSMediaPicker.h"
#import "YAYICache.h"
#import "YAYISkillModel.h"
//#import "YAYINetWorking.h"
//#import "MJExtension.h"


#define YAYISheetChosePhotoTag 400
#define YAYISheetChoseSexTag 401


@interface YAYIPersoninfoViewController () <YAYIEditNameViewControllerDelegate,UIActionSheetDelegate, FSMediaPickerDelegate, YAYISkillSelectViewControllerDelegate, YAYIEditBriefViewControllerDelegate, YAYIActionSheetDelegate>
/** 个人简介 */
@property (nonatomic, weak) UILabel *briefLbl;


@property (nonatomic, strong) NSIndexPath *sexIndexPath;
@property (nonatomic, strong) NSIndexPath *iconIndexPath;
@property (nonatomic, strong) NSIndexPath *ageIndexPath;


// 数据是否被修改过，如果是则刷新上一控制器
@property (nonatomic, assign) BOOL isChanged;

/** 职称 */
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSArray *skillArray;
@property (nonatomic, copy) NSString *briefStr;

/** 模型 */
@property (nonatomic, strong) YAYIUserinfoModel *userInfoModel;

@end


@implementation YAYIPersoninfoViewController

- (NSMutableArray *)titleArray
{
    if (nil == _titleArray) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (NSArray *)skillArray
{
    if (nil == _skillArray) {
//        _skillArray = @[@"超声波洁齿美白", @"多生牙", @"覆盖义齿", @"非龋齿", @"复杂智齿", @"多颗牙种植"];
    }
    
    return _skillArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人信息";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 20, 0);
    self.tableView.separatorInset = UIEdgeInsetsZero;
    [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    self.tableView.separatorColor = YAYICellLineColor;
    
    [self setupNav];
    // 添加组
    [self setupSection0];
    [self setupSection1];
    [self setupSection2];
    
    [self getDataByNetworking];
}


- (void)setupNav
{
    UIButton *backBtn = self.navigationItem.leftBarButtonItem.customView;
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)setupSection0
{

    __block typeof(self) weakself = self;
    
//    NSString *iconStr = [NSString stringWithFormat:@"%@%@", API, @"test"];
    YAYISettingItemModel *item0 = [YAYISettingItemModel itemWithIcon:nil name:@"头像"];
    item0.executeOperation = ^(NSIndexPath *indexPath){
        dispatch_async(dispatch_get_main_queue(), ^{
            UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从手机相册选择", nil];
            sheet.tag = YAYISheetChosePhotoTag;
            
            weakself.iconIndexPath = indexPath;
            
            [sheet showInView:self.view];
        });
    };
    
    YAYISettingItemModel *item1 = [YAYISettingItemModel itemWithIcon:nil name:@"账号"];
    item1.isHaveArrow = NO;
    
    YAYISettingSectionModel *section0 = [[YAYISettingSectionModel alloc]init];
    section0.itemArray = @[item0];
    
    [self.sectionArray addObject:section0];
}


- (void)setupSection1
{

    __block typeof(self) weakself = self;
    
    YAYISettingItemModel *item0 = [YAYISettingItemModel itemWithIcon:nil name:@"姓名"];
    item0.isHaveArrow = NO;
    item0.executeOperation = ^(NSIndexPath *indexPath){
        
        YAYIEditNameViewController *editVc = [[YAYIEditNameViewController alloc] init];
        [self setTitleView:editVc title:@"姓名"];
        editVc.indexPath = indexPath;
        editVc.delegate = weakself;
        editVc.modelItem = self.userInfoModel;
        
        [weakself.navigationController pushViewController:editVc animated:YES];
        
    };
    
    YAYISettingItemModel *item1 = [YAYISettingItemModel itemWithIcon:nil name:@"职称"];
    item1.executeOperation = ^(NSIndexPath *indexPath){
        [weakself getTitles];
    };
    
    
    YAYISettingItemModel *item2 = [YAYISettingItemModel itemWithIcon:nil name:@"实名认证"];
//    item2.destVcClass = [YAYICertificationViewController class];
    item2.executeOperation = ^(NSIndexPath *indexPath){
        
        YAYICertificationViewController *certificationVc = [[YAYICertificationViewController alloc] init];
        certificationVc.state = self.userInfoModel.v;
        [weakself.navigationController pushViewController:certificationVc animated:YES];
        
    };
    
    
    YAYISettingItemModel *item3 = [YAYISettingItemModel itemWithIcon:nil name:@"擅长项目"];
    item3.executeOperation = ^(NSIndexPath *indexPath){
        
        YAYISkillSelectViewController *skillSelectVc = [[YAYISkillSelectViewController alloc] init];
        skillSelectVc.delegate = weakself;
        if (self.userInfoModel) {
            skillSelectVc.doctorID = [NSString stringWithFormat:@"%ld", self.userInfoModel.id];
        }
        skillSelectVc.selectedItemArray = self.skillArray;

        [weakself.navigationController pushViewController:skillSelectVc animated:YES];
        
    };
    
    
    YAYISettingSectionModel *section1 = [[YAYISettingSectionModel alloc]init];
    section1.itemArray = @[item0, item1, item2, item3];
    
    [self.sectionArray addObject:section1];
}

- (void)setupSection2
{
    __block typeof(self) weakself = self;
    
    YAYISettingItemModel *item0 = [YAYISettingItemModel itemWithIcon:nil name:@"个人简介"];
    item0.executeOperation = ^(NSIndexPath *indexPath){
        
        YAYIEditBriefViewController *editBriefVc = [[YAYIEditBriefViewController alloc] init];
        editBriefVc.delegate = weakself;
        if (self.userInfoModel) {
            editBriefVc.doctorID = [NSString stringWithFormat:@"%ld", self.userInfoModel.id];
            editBriefVc.brief = self.userInfoModel.brief;
        }
        
        
        [weakself.navigationController pushViewController:editBriefVc animated:YES];
        
    };
    
    YAYISettingSectionModel *section2 = [[YAYISettingSectionModel alloc]init];
    section2.itemArray = @[item0];
    
    [self.sectionArray addObject:section2];
}


// 设置控制器的标题
- (void)setTitleView:(UIViewController *)nav title:(NSString *)title{
    UILabel *titleView = [[UILabel  alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
    titleView.textColor = [UIColor  whiteColor];
    titleView.text = title;
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.font = [UIFont  systemFontOfSize:18 weight:0];
    nav.navigationItem.titleView = titleView;
}


#pragma mark - 按钮相应方法
-(void)backAction:(UIButton *)sender
{
    if (self.refreshOperation && self.isChanged) {
//        self.refreshOperation();
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}


// 时间字符串转年龄
- (NSInteger)ageWithDateString:(NSString *)dateStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [formatter dateFromString:dateStr];
    
    return [self ageWithDateOfBirth:date];
}

//
- (NSInteger)ageWithDateOfBirth:(NSDate *)date;
{
    // 出生日期转换 年月日
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
    NSInteger brithDateYear  = [components1 year];
    NSInteger brithDateDay   = [components1 day];
    NSInteger brithDateMonth = [components1 month];
    
    // 获取系统当前 年月日
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
    NSInteger currentDateYear  = [components2 year];
    NSInteger currentDateDay   = [components2 day];
    NSInteger currentDateMonth = [components2 month];
    
    // 计算年龄
    NSInteger iAge = currentDateYear - brithDateYear - 1;
    if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
        iAge++;
    }
    
    return iAge;
}


#pragma mark - 获取数据
- (void)getDataByNetworking
{
    // 显示hud
    [SVProgressHUD show];
    
    [YAYINetWorking getWithURL:me_personalinfo param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                [SVProgressHUD dismiss];
                return;
            }
            
            YAYIUserinfoModel *userInfoModel = [[YAYIUserinfoModel alloc] init];
            [userInfoModel setValuesForKeysWithDictionary:dict[@"map"][@"account"]];
            [userInfoModel setValuesForKeysWithDictionary:dict[@"data"]];
            
            self.userInfoModel = userInfoModel;
            
            YAYISettingSectionModel *section = nil;
            YAYISettingItemModel *item = nil;
            
            // 姓名
            section = self.sectionArray[1];
            item = section.itemArray[0];
            item.detail = userInfoModel.fullname;//dict[@"map"][@"fullname"];
            
            
            // 职称
            section = self.sectionArray[1];
            item = section.itemArray[1];
            item.detail = userInfoModel.title;
            
            // 实名认证
            section = self.sectionArray[1];
            item = section.itemArray[2];
            NSArray *titleArray = @[@"未认证", @"审核中", @"已认证", @"认证失败"];
            item.detail = titleArray[userInfoModel.v];
            
            // 擅长项目
            NSArray *skillArry = dict[@"map"][@"dtDisease"];
            NSMutableArray *skillStrArray = [NSMutableArray array];
            for (NSDictionary *itemDict in skillArry) {
//                NSString *skillName = dict[@"diseaseName"];
                YAYISkillModel *model = [[YAYISkillModel alloc] init];
//                [model setValuesForKeysWithDictionary:itemDict];
                model.diseaseid = [itemDict[@"diseaseid"] integerValue];
                model.name = itemDict[@"diseaseName"];
                
                [skillStrArray addObject:model];
            }
            self.skillArray = [skillStrArray copy];
            
            // 简介
            if (userInfoModel.brief && userInfoModel.brief.length > 0) {
                self.briefStr = userInfoModel.brief;
            }
            
            
            // 头像
            section = self.sectionArray[0];
            item = section.itemArray[0];
            NSString *str = [NSString stringWithFormat:@"%@%@", API, userInfoModel.avatar];
            item.icon = str;
            
            [self.tableView reloadData];
        }else{
        
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] options:NSJSONReadingMutableContainers error:nil];
            YAYILog(@"dict:%@", dict);
            
            [SVProgressHUD showImage:nil status:[NSString stringWithFormat:@"\n\n%@\n\n", dict[@"message"]]];
        }
        
        // 取消显示hud
        [SVProgressHUD dismiss];
    }];
    
}


/**
 *  获取职称
 */
- (void)getTitles
{
    if (self.titleArray.count > 0) {
        YAYIActionSheet *actionSheet = [[YAYIActionSheet alloc] initWithDelegate:self cancelButtonTitle:@"取消" otherButtonTitles:self.titleArray];
        [actionSheet show];
        return;
    }
    
    
    [YAYINetWorking getWithURL:titleList_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"职称返回：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
            }else{
                NSDictionary *dictMap = dict[@"map"];
                NSArray *keyArray = [dictMap allKeys];
                YAYILog(@"keyArray:%@ %@", keyArray, [keyArray[0] class]);
                
                for (int i = 0; i < keyArray.count; i++) {
                    NSString *key = [NSString stringWithFormat:@"%d", i+1];
                    NSString *typeStr = dictMap[key];
                    
                    [self.titleArray addObject:typeStr];
                }
                
                YAYIActionSheet *actionSheet = [[YAYIActionSheet alloc] initWithDelegate:self cancelButtonTitle:@"取消" otherButtonTitles:self.titleArray];
                [actionSheet show];
                
            }
        }
    }];
}


#pragma mark - 工具方法
/**
 *  压缩图片
 *
 *  @param image       被压缩图片
 *  @param maxFileSize 压缩后尺寸
 *
 */
- (UIImage *)compressImage:(UIImage *)image toSize:(CGSize)imageSize toMaxFileSize:(NSInteger)maxFileSize
{
    
    UIGraphicsBeginImageContext(CGSizeMake(imageSize.width, imageSize.height));
    [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGFloat compression = 0.9f;
    CGFloat maxCompression = 0.1f;
    NSData *imageData = UIImageJPEGRepresentation(newImage, compression);
    
    YAYILog(@"imageData length:%ld", imageData.length);
    
    while ([imageData length] > maxFileSize && compression > maxCompression) {
        compression -= 0.1;
        imageData = UIImageJPEGRepresentation(image, compression);
        YAYILog(@"imageData length:%ld", imageData.length);
    }
    
    UIImage *compressedImage = [UIImage imageWithData:imageData];
    return compressedImage;
}


#pragma mark - tableview数据源方法
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (1 == section) {
        CGFloat skillViewH = [YAYIShowSkillView getHightWiht:self.skillArray];
        return skillViewH < 80 ? 80 : skillViewH + 10;
        
    }else if (2 == section) {
        NSString *desStr = @"请介绍自己的从业经验和特长，以方便患者了解您。";
        if (self.briefStr) {
            desStr = self.briefStr;
        }
        CGSize desStrSize = [desStr sizewithFont:[UIFont systemFontOfSize:15] maxSize:CGSizeMake(SCREEN_width - 2 * 15, CGFLOAT_MAX)];
        
        return desStrSize.height + 2*15 + 10;
    }
    
    return 0;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (1 == section) {
        CGFloat height = [YAYIShowSkillView getHightWiht:self.skillArray];
        height = height < 80 ? 80 : height + 10;
        
        YAYIShowSkillView *showView = [[YAYIShowSkillView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_width, height)];
        showView.backgroundColor = [UIColor whiteColor];
        showView.skillArray = self.skillArray;
        
        return showView;
        
        
    }else if (2 == section) {
        NSString *desStr = @"请介绍自己的从业经验和特长，以方便患者了解您。";
        if (self.briefStr) {
            desStr = self.briefStr;
        }
        
        CGSize desStrSize = [desStr sizewithFont:[UIFont systemFontOfSize:15] maxSize:CGSizeMake(SCREEN_width - 2 * 15, CGFLOAT_MAX)];
        
        UIView *footerView = [[UIView alloc] init];
        footerView.backgroundColor = [UIColor whiteColor];
        footerView.frame = CGRectMake(0, 0, SCREEN_width, desStrSize.height + 15 * 2 + 10);
        
        UILabel *desLbl = [[UILabel alloc] init];
        desLbl.numberOfLines = 0;
        desLbl.text = desStr;
        desLbl.textColor = YAYIColor(160, 163, 163);
        desLbl.font = [UIFont systemFontOfSize:15];
        [footerView addSubview:desLbl];
        self.briefLbl = desLbl;
        [desLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@15);
            make.right.equalTo(@-15);
            make.top.equalTo(@0);
            make.height.equalTo(footerView).offset(-10);
        }];
        
        return footerView;
    }
    
    return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 去除数据模型
    YAYISettingSectionModel *section = self.sectionArray[indexPath.section];
    YAYISettingItemModel *model = section.itemArray[indexPath.row];
    
    if (indexPath.section == 0 && 0 == indexPath.row) {
        YAYIPersoninfoHeadCell *cell = [YAYIPersoninfoHeadCell cellWith:tableView];
        cell.model = model;
        [cell setLayoutMargins:UIEdgeInsetsZero];
        return cell;
    }
    
    YAYIPersoninfoSetCell *cell = [YAYIPersoninfoSetCell cellWith:tableView];
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && 0 == indexPath.row) {
        return 90;
    }
    
    return 50;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
//    if (1 == indexPath.section && 1 == indexPath.row) {
//        [self getTitles];
//    }
}


#pragma mark actionSheet代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    YAYILog(@"actionSheet:%ld", buttonIndex);
    switch (actionSheet.tag) {
        // actionSheet
        case YAYISheetChosePhotoTag:{
                if (0 == buttonIndex) {
                    FSMediaPicker *mediaPicker = [[FSMediaPicker alloc] init];
                    mediaPicker.mediaType = FSMediaTypePhoto;
                    mediaPicker.editMode = FSEditModeStandard;
                    mediaPicker.delegate = self;
                    //                [mediaPicker showFromView:self.view];
                    [mediaPicker takePhotoFromCamera];
                }else if (1 == buttonIndex){
                
                    FSMediaPicker *mediaPicker = [[FSMediaPicker alloc] init];
                    mediaPicker.mediaType = FSMediaTypePhoto;
                    mediaPicker.editMode = FSEditModeStandard;
                    mediaPicker.delegate = self;
                    //                [mediaPicker showFromView:self.view];
                    [mediaPicker takePhotoFromPhotoLibrary];
                }
            }
            break;
        // 选择性别
        case YAYISheetChoseSexTag:
            if (0 == buttonIndex) {
                // 取出数据模型
                YAYISettingSectionModel *goup = self.sectionArray[self.sexIndexPath.section];
                YAYISettingItemModel *item = goup.itemArray[self.sexIndexPath.row];
                
                item.name = @"男";
                [self.tableView reloadRowsAtIndexPaths:@[self.sexIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }else if (1 == buttonIndex){
                // 取出数据模型
                YAYISettingSectionModel *goup = self.sectionArray[self.sexIndexPath.section];
                YAYISettingItemModel *item = goup.itemArray[self.sexIndexPath.row];
                
                item.name = @"女";
                [self.tableView reloadRowsAtIndexPaths:@[self.sexIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
            
            break;
            
        default:
            break;
    };
}

#pragma mark YAYIEditNameViewController代理方法
- (void)editNameDidChanged:(NSString *)name indexPath:(NSIndexPath *)indexPath
{
    YAYILog(@"return  name:%@", name);

    self.isChanged = YES;
    [self getDataByNetworking];
}

#pragma mark FSMediaPicker代理方法
- (void)mediaPicker:(FSMediaPicker *)mediaPicker didFinishWithMediaInfo:(NSDictionary *)mediaInfo
{
    if (mediaInfo.mediaType == FSMediaTypeVideo) {

    } else {
        if (mediaPicker.editMode == FSEditModeNone) {
            
        } else {
            UIImage *image = mediaInfo.editedImage;
            
            NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
            
            UIImage *newImage = [self compressImage:image toSize:CGSizeMake(100, 100) toMaxFileSize:200 * 1024];
            NSData *newImgData = UIImageJPEGRepresentation(newImage, 1.0);
            
            YAYILog(@"imageData:%ld, newImgData:%ld", imgData.length, newImgData.length);
            
//            [YAYICache setObject:imgData forKey:@"head.png"];
            
//            NSString *cachePath = [YAYICache cacheDirectory];
//            NSString *path = [cachePath stringByAppendingPathComponent:@"head.png"];
//            
//            YAYISettingSectionModel *section = self.sectionArray[0];
//            YAYISettingItemModel *item = section.itemArray[0];
//            item.icon = path;
//            
//            NSIndexPath *headIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//            [self.tableView reloadRowsAtIndexPaths:@[headIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            NSString *username = [userDefault stringForKey:YAYIUsernameKey];
            
            // 清除图片缓存
            NSString *headUrlStr = [NSString stringWithFormat:@"%@%@", API, self.userInfoModel.avatar];
            [[SDImageCache sharedImageCache] removeImageForKey:headUrlStr];
            
            // 1.创建请求管理对象
            AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
            
            NSString *urlStr = [NSString stringWithFormat:@"%@%@", API, me_uploadHead];
            // 2.发送请求
            [mgr POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                [formData appendPartWithFileData:imgData name:@"head" fileName:@"head.jpeg" mimeType:@"image/jpeg"];
            } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                YAYILog(@"response:%@", responseObject);
                
                // 清除IM头像缓存

                self.isChanged = YES;
                [self getDataByNetworking];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                 YAYILog(@"response:%@", error);
            }];
        }
    }
}


#pragma mark YAYIActionSheet代理
- (void)actionSheet:(YAYIActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)index
{
    YAYISettingSectionModel *section = self.sectionArray[1];
    YAYISettingItemModel *item = section.itemArray[1];
    item.detail = self.titleArray[index];

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = [NSString stringWithFormat:@"%ld", self.userInfoModel.id];
    param[@"title"] = [NSString stringWithFormat:@"%ld", index+1];
    
    YAYILog(@"param:%@", param);
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
    [YAYINetWorking postJsonSerilizeWithURL:me_editUserinfo param:param finish:^(NSData *data, NSDictionary *dict, NSError *error,NSDictionary *responseError) {
        if (error == nil) {
            YAYILog(@"保存标签：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                [SVProgressHUD showWithStatus:@"保存失败"];
                
            }else{
                [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                
                self.isChanged = YES;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
                    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                    
                });
            }
        }else{
            NSString *message = responseError[@"message"];
            [SVProgressHUD showWithStatus:message];
        }
    }];
}


#pragma mark YAYISkillSelectViewController代理
- (void)skillSelectSaved:(NSArray *)skillNameArray
{
    self.skillArray = skillNameArray;

    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
}



#pragma mark YAYIEditNameViewControllerDelegate代理
- (void)editNameDidChanged:(NSString *)name
{
    self.userInfoModel.fullname = name;
    
    self.isChanged = YES;
    YAYISettingSectionModel *section = self.sectionArray[1];
    YAYISettingItemModel *item = section.itemArray[0];
    item.detail = name;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}


#pragma mark YAYIEditBriefViewControllerDelegate代理
- (void)editBriefDidChanged:(NSString *)name
{
    self.userInfoModel.brief = name;
    
    self.briefStr = name;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade];
}



@end
